#pragma once
#include <curl/curl.h>
#include <utility>  // for std::swap

class CurlHandle {
public:
    // 构造时初始化句柄，析构时自动释放
    CurlHandle() : m_handle(curl_easy_init()) {}
    ~CurlHandle() { if (m_handle) curl_easy_cleanup(m_handle); }

    // 禁止拷贝（避免句柄重复释放）
    CurlHandle(const CurlHandle&) = delete;
    CurlHandle& operator=(const CurlHandle&) = delete;

    // 允许移动（支持资源转移）
    CurlHandle(CurlHandle&& other) noexcept : m_handle(other.m_handle) {
        other.m_handle = nullptr;  // 转移所有权后，原对象不再持有句柄
    }
    CurlHandle& operator=(CurlHandle&& other) noexcept {
        std::swap(m_handle, other.m_handle);
        return *this;
    }

    // 获取原生句柄（类似智能指针的get()）
    CURL* get() const { return m_handle; }
    explicit operator bool() const { return m_handle != nullptr; }  // 支持判断句柄是否有效

private:
    CURL* m_handle = nullptr;
};

// 同理封装curl_slist头信息
class CurlSlist {
public:
    CurlSlist() = default;
    ~CurlSlist() { if (m_list) curl_slist_free_all(m_list); }

    // 禁止拷贝，允许移动（同上）
    CurlSlist(const CurlSlist&) = delete;
    CurlSlist& operator=(const CurlSlist&) = delete;
    CurlSlist(CurlSlist&& other) noexcept : m_list(other.m_list) {
        other.m_list = nullptr;
    }
    CurlSlist& operator=(CurlSlist&& other) noexcept {
        std::swap(m_list, other.m_list);
        return *this;
    }

    // 封装curl_slist_append（自动管理链表生命周期）
    void append(const char* header) {
        m_list = curl_slist_append(m_list, header);
    }

    struct curl_slist* get() const { return m_list; }

private:
    struct curl_slist* m_list = nullptr;
};

